package edu.pitt.myptm.datamanager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import edu.pitt.myptm.DataItem;
import edu.pitt.myptm.utils.DBUtils;

public class DataLoader {
	public static void testRead(DataManager dm,String filename) {
		System.out.println(dm.getFileOperator(filename).readItem(1912));
	}
	
	public static void loadFile(String filename,int pageLimit) {
		String toFile = DBUtils.genFilename(filename);
		DataManager dm = new DataManager();
		dm.setPageLimit(pageLimit);
		dm.addFileOperator(toFile);
		dm.getFileOperator(toFile).deleteFile();
		dm.addFileOperator(toFile);
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
			String line = reader.readLine();
			while(line!=null) {
				String[] subs = line.split(",");
				DataItem item = new DataItem();
				item.setId(Integer.parseInt(subs[0].trim()));
				int clientLength = item.clientLength;
				byte[] clientName = new byte[clientLength];
				byte[] clientbytes = subs[1].trim().getBytes();
				if(clientLength>clientbytes.length) clientLength = clientbytes.length;
				for(int i = 0;i<clientLength;i++) {
					clientName[i] = clientbytes[i];
				}
				item.setClientName(clientName);
				int phoneLength = item.phoneLength;
				byte[] phonebytes = subs[2].trim().getBytes();
				byte[] phone = new byte[phoneLength];
				if(phoneLength>phonebytes.length) phoneLength = phonebytes.length;
				for(int i= 0;i<phoneLength;i++) {
					phone[i] = phonebytes[i];
				}
				item.setPhone(phone);
				dm.getFileOperator(toFile).writeItem(item);
				line = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String [] args) {
		/*File f = new File("sampleDataFile2.txt");
		DataManager dm = new DataManager();
		dm.setPageLimit(1024);
		String toFile = "sampleDataFile.txt";
		dm.addFileOperator(toFile);
		dm.getFileOperator(toFile).deleteFile();
		dm.addFileOperator(toFile);
		try {
			BufferedReader reader = new BufferedReader(new FileReader(f));
			String line = reader.readLine();
			while(line!=null) {
				String[] subs = line.split(",");
				DataItem item = new DataItem();
				item.setId(Integer.parseInt(subs[0]));
				int clientLength = item.clientLength;
				byte[] clientName = new byte[clientLength];
				byte[] clientbytes = subs[1].getBytes();
				if(clientLength>clientbytes.length) clientLength = clientbytes.length;
				for(int i = 0;i<clientLength;i++) {
					clientName[i] = clientbytes[i];
				}
				item.setClientName(clientName);
				int phoneLength = item.phoneLength;
				byte[] phonebytes = subs[2].getBytes();
				byte[] phone = new byte[phoneLength];
				if(phoneLength>phonebytes.length) phoneLength = phonebytes.length;
				for(int i= 0;i<phoneLength;i++) {
					phone[i] = phonebytes[i];
				}
				item.setPhone(phone);
				dm.getFileOperator(toFile).writeItem(item);
				line = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		loadFile("ds1/X.txt",8000);
		DataManager dm = new DataManager(1024);
		dm.setPageLimit(8000);
		testRead(dm,"ds1/X.mpm");
	}
}
